package com.itboye.ihomebank.adapter;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.database.CursorJoiner.Result;
import android.view.View;
import android.view.View.OnClickListener;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.ZuKeBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.RoundImageView;
import com.itboye.ihomebank.presenter.HousePresenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.TimesUtils;
import com.itboye.ihomebank.util.XImageLoader;

/***
 * 示例adapter
 * 
 * @author mr.w
 * 
 */
public class XuanZuKeAdapter extends MutiplyCommonAdapter<ZuKeBean> implements
		Observer {

	int position;
	Activity context;
	protected HousePresenter housepPresenter;
	List<ZuKeBean> datas;
	protected String shenQingId;
	protected String uid;

	public XuanZuKeAdapter(Activity context, List<ZuKeBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.datas = datas;
		housepPresenter = new HousePresenter(this);
		uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub

		return 0;
	}

	@Override
	public void convert(ViewHolder holder, final ZuKeBean t, int type,
			final int position) {
		// TODO Auto-generated method stub
		if (t.getHead() != null) {
			if (!t.getHead().equals(""))
				XImageLoader.load(t.getHead(),
						(RoundImageView) holder.getView(R.id.roundImage));
		}
		holder.setText(R.id.name,
				"租客姓名：" + (t.getRealName() == null ? "" : t.getRealName()));
		holder.setText(R.id.mobile, "租客电话：" + t.getMobile());
		if (t.getApplyTime() != null) {
			holder.setText(
					R.id.time,
					"申请时间："
							+ TimesUtils.timeStamp2Date(Long.parseLong(t
									.getApplyTime())));
		}
		holder.setText(R.id.beizhu, "起租时间：" + t.getStarttime() + "\n\n" + "止租时间："
				+ t.getEndtime() + "\n\n" + "意向月租金：" + t.getMoney() + "\n\n"
				+ "付款方式：" + t.getPaytype());
		holder.setOnClickListener(R.id.jieshou, position,
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						shenQingId = t.getId();
						housepPresenter.xuanZuKe(t.getUid(), uid,
								t.getHouseNo(), "1");
					}
				});
	}

	@Override
	public void getItemPosition(int position) {
		// TODO Auto-generated method stub
		this.position = position;
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == HousePresenter.xuanZuKe_success) {
				ByAlert.alert(entity.getData());
				Iterator<ZuKeBean> iterator = datas.iterator();
				while (iterator.hasNext()) {
					ZuKeBean type = (ZuKeBean) iterator.next();
					if (type.getId() == shenQingId) {
						iterator.remove();
						notifyDataSetChanged();
						break;
					}

				}
			} else if (entity.getEventType() == HousePresenter.xuanZuKe_fail) {
				ByAlert.alert(entity.getData());
			}
		}
	}

}
